package com.mogujie.tt.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.util.Log;

public class JsonUtil {
	private JSONObject jsonObject;

	/**
	 * get json object
	 * 
	 * @param json
	 *            json data
	 * @return JOSNObject
	 */
	public JSONObject getJsonObject(String json) {
		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(json);
		} catch (JSONException e) {
			
		}
		return jsonObject;
	}

 
	
	
	/**
	 * get String data
	 * 
	 *
	 *            json data
	 * @param key
	 *            param
	 * @return String data
	 * @throws JSONException
	 */
	public String getString(String key) throws JSONException {
		if (jsonObject != null) {
			return jsonObject.getString(key);
		} else {
			return null;
		}

	}

	/**
	 * get String data
	 * 
	 *
	 *            json data
	 * @param key
	 *            param
	 * @return int data
	 * @throws JSONException
	 */
	public int getInt(String key) throws JSONException {
		if (jsonObject != null) {
			return jsonObject.getInt(key);
		} else {
			return -1;
		}

	}

	/**
	 * get Double data
	 * 
	 *
	 *            json data
	 * @param key
	 *            param
	 * @return double data
	 * @throws JSONException
	 */
	public double getDouble(String key) throws JSONException {
		if (jsonObject != null) {
			return jsonObject.getDouble(key);
		} else {
			return -1;
		}

	}

	/**
	 * This Method use in jsonObject get current class with object
	 * 
	 *
	 * @param c
	 *            class
	 * @return object
	 * @throws Exception
	 */
	public Object getObject(Class<?> c) throws Exception {
		if (jsonObject != null) {
			return getObject(c.getSimpleName().toLowerCase(), c);
		} else {
			return null;
		}
	}

	/**
	 * This Method use in jsonObject get current class with object
	 * 
	 *
	 * @param key
	 *            query key
	 * @param c
	 *            class
	 * @return object
	 * @throws Exception
	 */
	public Object getObject(String key, Class<?> c) throws Exception {
		if (jsonObject != null) {
			return getObject(jsonObject, key, c);
		} else {
			return null;
		}
	}

	public Object getObject(JSONObject jsonObject, Class<?> c) throws Exception {
		return getObject(jsonObject, c.getSimpleName().toLowerCase(), c);
	}

	/**
	 * This Method use in jsonObject get current class with object
	 * 
	 * @param jsonObject
	 * @param key
	 *            query key
	 * @param c
	 *            class
	 * @return object
	 * @throws Exception
	 */
	public Object getObject(JSONObject jsonObject, String key, Class<?> c)
			throws Exception {
		Object bean = null;

		if (jsonObject != null) {
			JSONObject jo = null;
			if (key != null) {
				jo = jsonObject.getJSONObject(key);
			} else {
				jo = jsonObject;
			}
			if (jo != null) {
				if (c.equals(null)) {
					bean = jo.get(key);
				} else {
					bean = c.newInstance();
					Field[] fs = c.getDeclaredFields();
					for (int i = 0; i < fs.length; i++) {
						Field f = fs[i];
						f.setAccessible(true);
						Type type = f.getGenericType();
						String value = jo.getString(f.getName());
						if (type.equals(int.class)) {
							f.setInt(bean, Integer.valueOf(value));
						} else if (type.equals(double.class)) {
							f.setDouble(bean, Double.valueOf(value));
						} else {
							f.set(bean, value);
						}
					}
				}
			} else {
			}
		} else {
		}
		return bean;
	}

	/**
	 * This method use in jsonObject get list object
	 * 
	 * @param key
	 *            list key
	 *
	 *            object key
	 * @param c
	 *            object
	 * @return list
	 * @throws Exception
	 */
	public List<Object> getList(String key, Class<?> c) throws Exception {
		List<Object> list = null;
		if (jsonObject != null) {
			JSONArray jsonArray = jsonObject.getJSONArray(key);
			if (!jsonArray.isNull(0)) {
				list = new ArrayList<Object>();
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsObject = jsonArray.getJSONObject(i);
					Object object = getObject(jsObject, null, c);
					list.add(object);
				}
			}
		}
		return list;
	}

	/**
	 * Test class field value
	 * 
	 * @param c
	 * @param classObject
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static String getFieldValue(Class<?> c, Object classObject)
			throws IllegalArgumentException, IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		Field[] fs = c.getFields();
		for (int i = 0; i < fs.length; i++) {
			String s = fs[i].getName() + "=" + fs[i].get(classObject);
			sb.append(s).append("\n");
		}
		// Log.e(TAG, sb.toString());
		return sb.toString();
	}
	
	 
	@SuppressLint("SimpleDateFormat")
	public static JSONObject jsonRequest(String serviceName,
			String operationName, Map<String, Object> params) {

		JSONObject jsonObject = new JSONObject();
		JSONObject request = new JSONObject();
		JSONObject content = new JSONObject();
		try {

			request.put("service_name", serviceName);//服务器名称
			request.put("operation_name", operationName);//更新选项

	 
			String time = new SimpleDateFormat("yyyyMMddHHmmss")
					.format(new Date());
			//Log.v("","time = "+time);
			request.put("timestamp", time);
			
			request.put("request_seq", time);
			
          //  Log.v("","request = "+request);
            
			if (!params.isEmpty()) {
				for (String key : params.keySet()) {//读出请求参数
					String value = params.get(key).toString();
					content.put(key, value);
				}
			}
			Log.v("","content = "+content);
			request.put("content", "{\"input\":" + content.toString() + "}");
			jsonObject.put("request", request);//连接

		} catch (JSONException e) {
		}

		return jsonObject;

	}
	
	
	public JSONObject getOutputJson(String jsonstr) {

		//JsonUtil jsonUtil = new JsonUtil();
		JSONObject outputtrsjson = null;

		if(jsonstr!=null){
			try {
			//	Log.v("1111111111","jsonstr = "+jsonstr);
				JSONObject jsonObject = this.getJsonObject(jsonstr);//建立json格式
				
				JSONObject responsejson = jsonObject.getJSONObject("response");
			//	Log.v("","responsejson = "+responsejson);
				
				String contentstr = responsejson.getString("content");
			//	Log.v("","contentstr = "+contentstr);
				
				JSONObject contentjson = this.getJsonObject(contentstr);
			//	Log.v("","contentjson = "+contentjson);
				
				String outputtrs = contentjson.getString("output");
			//	Log.v("","outputtrs = "+outputtrs);

				outputtrsjson = this.getJsonObject(outputtrs);
	
			} catch (JSONException e) {
			}
		}
		return outputtrsjson;
	}

}
